package cn.stylefeng.guns.modular.forecastClineInformation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.forecastClineInformation.entity.QyuanPowerSdClineForecast;
import cn.stylefeng.guns.modular.forecastClineInformation.enums.QyuanPowerSdClineForecastExceptionEnum;
import cn.stylefeng.guns.modular.forecastClineInformation.mapper.QyuanPowerSdClineForecastMapper;
import cn.stylefeng.guns.modular.forecastClineInformation.pojo.request.QyuanPowerSdClineForecastRequest;
import cn.stylefeng.guns.modular.forecastClineInformation.service.QyuanPowerSdClineForecastService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Date;
import java.math.BigDecimal;

/**
 * 电网联络线预测信息表，记录每天96个时间点的电网运行预测数据业务实现层
 *
 * @author 我是谭先生🐻
 * @date 2025/03/14 16:08
 */
@Service
public class QyuanPowerSdClineForecastServiceImpl extends ServiceImpl<QyuanPowerSdClineForecastMapper, QyuanPowerSdClineForecast> implements QyuanPowerSdClineForecastService {

	@Override
    public void add(QyuanPowerSdClineForecastRequest qyuanPowerSdClineForecastRequest) {
        QyuanPowerSdClineForecast qyuanPowerSdClineForecast = new QyuanPowerSdClineForecast();
        BeanUtil.copyProperties(qyuanPowerSdClineForecastRequest, qyuanPowerSdClineForecast);
        this.save(qyuanPowerSdClineForecast);
    }

    @Override
    public void del(QyuanPowerSdClineForecastRequest qyuanPowerSdClineForecastRequest) {
        QyuanPowerSdClineForecast qyuanPowerSdClineForecast = this.queryQyuanPowerSdClineForecast(qyuanPowerSdClineForecastRequest);
        this.removeById(qyuanPowerSdClineForecast.getId());
    }

    @Override
    public void edit(QyuanPowerSdClineForecastRequest qyuanPowerSdClineForecastRequest) {
        QyuanPowerSdClineForecast qyuanPowerSdClineForecast = this.queryQyuanPowerSdClineForecast(qyuanPowerSdClineForecastRequest);
        BeanUtil.copyProperties(qyuanPowerSdClineForecastRequest, qyuanPowerSdClineForecast);
        this.updateById(qyuanPowerSdClineForecast);
    }

    @Override
    public QyuanPowerSdClineForecast detail(QyuanPowerSdClineForecastRequest qyuanPowerSdClineForecastRequest) {
        return this.queryQyuanPowerSdClineForecast(qyuanPowerSdClineForecastRequest);
    }

    @Override
    public PageResult<QyuanPowerSdClineForecast> findPage(QyuanPowerSdClineForecastRequest qyuanPowerSdClineForecastRequest) {
        LambdaQueryWrapper<QyuanPowerSdClineForecast> wrapper = createWrapper(qyuanPowerSdClineForecastRequest);
        Page<QyuanPowerSdClineForecast> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<QyuanPowerSdClineForecast> findList(QyuanPowerSdClineForecastRequest qyuanPowerSdClineForecastRequest) {
        LambdaQueryWrapper<QyuanPowerSdClineForecast> wrapper = this.createWrapper(qyuanPowerSdClineForecastRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author 我是谭先生🐻
     * @date 2025/03/14 16:08
     */
    private QyuanPowerSdClineForecast queryQyuanPowerSdClineForecast(QyuanPowerSdClineForecastRequest qyuanPowerSdClineForecastRequest) {
        QyuanPowerSdClineForecast qyuanPowerSdClineForecast = this.getById(qyuanPowerSdClineForecastRequest.getId());
        if (ObjectUtil.isEmpty(qyuanPowerSdClineForecast)) {
            throw new ServiceException(QyuanPowerSdClineForecastExceptionEnum.QYUAN_POWER_SD_CLINE_FORECAST_NOT_EXISTED);
        }
        return qyuanPowerSdClineForecast;
    }

    /**
     * 创建查询wrapper
     *
     * @author 我是谭先生🐻
     * @date 2025/03/14 16:08
     */
    private LambdaQueryWrapper<QyuanPowerSdClineForecast> createWrapper(QyuanPowerSdClineForecastRequest qyuanPowerSdClineForecastRequest) {
        LambdaQueryWrapper<QyuanPowerSdClineForecast> queryWrapper = new LambdaQueryWrapper<>();

        Integer id = qyuanPowerSdClineForecastRequest.getId();
        String usageDate = qyuanPowerSdClineForecastRequest.getUsageDate();
        Integer timePoint = qyuanPowerSdClineForecastRequest.getTimePoint();
        String timeSlot = qyuanPowerSdClineForecastRequest.getTimeSlot();
        BigDecimal powerSupplyFromNorthchina = qyuanPowerSdClineForecastRequest.getPowerSupplyFromNorthchina();
        BigDecimal powerSupplyFromYindong = qyuanPowerSdClineForecastRequest.getPowerSupplyFromYindong();
        BigDecimal powerSupplyFromLugu = qyuanPowerSdClineForecastRequest.getPowerSupplyFromLugu();
        BigDecimal powerSupplyFromZhaoyi = qyuanPowerSdClineForecastRequest.getPowerSupplyFromZhaoyi();
        String createdAt = qyuanPowerSdClineForecastRequest.getCreateTime();
        String updatedAt = qyuanPowerSdClineForecastRequest.getUpdateTime();

        queryWrapper.eq(ObjectUtil.isNotNull(id), QyuanPowerSdClineForecast::getId, id);
        queryWrapper.eq(ObjectUtil.isNotNull(usageDate), QyuanPowerSdClineForecast::getUsageDate, usageDate);
        queryWrapper.eq(ObjectUtil.isNotNull(timePoint), QyuanPowerSdClineForecast::getTimePoint, timePoint);
        queryWrapper.like(ObjectUtil.isNotEmpty(timeSlot), QyuanPowerSdClineForecast::getTimeSlot, timeSlot);
        queryWrapper.eq(ObjectUtil.isNotNull(powerSupplyFromNorthchina), QyuanPowerSdClineForecast::getPowerSupplyFromNorthchina, powerSupplyFromNorthchina);
        queryWrapper.eq(ObjectUtil.isNotNull(powerSupplyFromYindong), QyuanPowerSdClineForecast::getPowerSupplyFromYindong, powerSupplyFromYindong);
        queryWrapper.eq(ObjectUtil.isNotNull(powerSupplyFromLugu), QyuanPowerSdClineForecast::getPowerSupplyFromLugu, powerSupplyFromLugu);
        queryWrapper.eq(ObjectUtil.isNotNull(powerSupplyFromZhaoyi), QyuanPowerSdClineForecast::getPowerSupplyFromZhaoyi, powerSupplyFromZhaoyi);
        queryWrapper.eq(ObjectUtil.isNotNull(createdAt), QyuanPowerSdClineForecast::getCreateTime, createdAt);
        queryWrapper.eq(ObjectUtil.isNotNull(updatedAt), QyuanPowerSdClineForecast::getUpdateTime, updatedAt);

        return queryWrapper;
    }

}